package tuiLayer;

import java.util.Scanner;
import controlLayer.*;
import modelLayer.*;


public class LoanUI {
    private PersonUI person;
    private DvdUI dvd;
    private PersonCtrl persCtrl;
    private LoanCtrl loanCtrl;
    private DvdCtrl dvdCtrl;
    private Dvd d;
    private Copy c;
    
    public LoanUI() {
        person = new PersonUI();
        persCtrl = new PersonCtrl();
        dvdCtrl = new DvdCtrl();
        loanCtrl = new LoanCtrl();
        dvd = new DvdUI();
        
    }
    
    public void loanMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeLoanMenu();
            
            switch(choice) {
            case 1 : createLoan();
                    stop = true;
                break;
            case 2 : findLoan();
                break;
            case 3 : returnLoan();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeLoanMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Loan Menu ***");
        System.out.println("1. Create loan ");
        System.out.println("2. Find loan ");
        System.out.println("3. Remove loan ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    private void createLoan() {
        String phone = person.inputPhone();
        Person p = (persCtrl.findPerson(phone));
        if(p != null) {
            int barcode = dvd.inputBarcode();
            d = (dvdCtrl.findDvd(barcode));
            if(d != null) {
                c = assignCopy();
                if(c == null || d.getCopiesNo() <= 0) {
                    System.out.println();
                    System.out.println("There are no available copies of the selected DVD.");
                }
                else {
                int id = inputId();
                String borrowDate = inputBorrowDate();
                String period = inputPeriod();
                d.setCopiesNo(d.getCopiesNo()-1);
                c.setAvailability(false);
                
                loanCtrl.createLoan(p, d, id, borrowDate, period);
                System.out.println();
                System.out.println("The loan was successfully created.");
                System.out.println();
                }
            }
            else {
                System.out.println("No DVD was found with the specified barcode");
            }
        }
        else {
            System.out.println("No person was found with the specified phone number");
        }
        MainMenuUI.pause();
    }
    
    private void findLoan() {
        int id = inputId();
        Loan l = (loanCtrl.findLoan(id));
        if (l != null) {
            System.out.println();
            showBorrowDate(l.getBorrowDate());
            showPeriod(l.getPeriod());
            showState(l.getState());
            System.out.println("DVD title : " + d.getTitle());
            System.out.println(" <<Copy lend>> ");
            System.out.println("Copy serial number : " + c.getSerialNumber());
            dvd.showPurchaseDate(c.getPurchaseDate());
            dvd.showPurchasePrice(c.getPurchasePrice());
        }
        else {
            System.out.println("No loan was found with the specified id.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void returnLoan() {
        Scanner keyboard = new Scanner(System.in);
        int id = inputId();
        Loan l = (loanCtrl.findLoan(id));
        boolean stop = false;
        if (l != null) {
            System.out.println();
            showBorrowDate(l.getBorrowDate());
            showPeriod(l.getPeriod());
            showState(l.getState());
            
            System.out.println();
            System.out.println("1. Return loan");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                    case 1 : loanCtrl.returnLoan(l);
                             c.setAvailability(true);
                             System.out.println();
                             System.out.println("The loan was returned");
                             System.out.println();
                             d.setCopiesNo(d.getCopiesNo()+1);
                             MainMenuUI.pause();
                        break;
                    case 0 : stop = true;
                        break;
                }
            }
        }
        else {
            System.out.println("No loan was found with the specified id.");
        }
    }
    
    private int inputId() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an id (numeric value) for the loan : ");
        int id = keyboard.nextInt();
        return id;
    }
    
    private String inputBorrowDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the borrow date : ");
        String borrowDate = keyboard.nextLine();
        return borrowDate;
    }
    
    private String inputPeriod() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the loan period (number of days): ");
        String period = keyboard.nextLine();
        return period;
    }
    
    private void showBorrowDate(String borrowDate)
    {
        System.out.println("BorrowDate date : " + borrowDate);
    }
    
    private void showPeriod(String period)
    { 
        System.out.println("Loan period : " + period);
    }
    
    private void showState(String state)
    { 
        System.out.println("State : " + state);
    }
    
    private Copy assignCopy() {
        boolean found = false;
        int i = 0;
        Copy c = null;
        while(i < d.copies.size() && !found) {
            c = d.copies.get(i);
            if(c.getAvailability()) {
                found = true;
            }
            else {
                i++;
            }
        }
        return c;
    }
         
}
